package com.ruoyi.finance.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.finance.service.IReciveOrderService;
import com.ruoyi.system.api.domain.finance.ReciveOrder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 财务收款单Controller
 *
 * @author wl
 * @date 2024-11-02
 */
@RestController
@RequestMapping("/reciveOrder")
@Slf4j
public class ReciveOrderController extends BaseController {
    @Autowired
    private IReciveOrderService reciveOrderService;

    /**
     * 查询财务收款单列表
     */
    @RequiresPermissions("finance:reciveOrder:list")
    @GetMapping("/list")
    public TableDataInfo list(ReciveOrder reciveOrder) {
        log.info("ReciveOrderController list reciveOrder:{}", reciveOrder);
        startPage();
        List<ReciveOrder> list = reciveOrderService.selectReciveOrderList(reciveOrder);
        TableDataInfo tableDataInfo = getDataTable(list);
        log.info("ReciveOrderController list tableDataInfo:{}", tableDataInfo);
        return tableDataInfo;
    }

    /**
     * 查询所有财务收款单列表
     */
    @RequiresPermissions("finance:reciveOrder:list")
    @GetMapping("/listAll")
    public TableDataInfo listAll(ReciveOrder reciveOrder) {
        log.info("ReciveOrderController listAll reciveOrder:{}", reciveOrder);
        List<ReciveOrder> list = reciveOrderService.selectReciveOrderList(reciveOrder);
        TableDataInfo tableDataInfo = getDataTable(list);
        log.info("ReciveOrderController listAll tableDataInfo:{}", tableDataInfo);
        return tableDataInfo;
    }

    /**
     * 导出财务收款单列表
     */
    @RequiresPermissions("finance:reciveOrder:export")
    @Log(title = "财务收款单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ReciveOrder reciveOrder) {
        List<ReciveOrder> list = reciveOrderService.selectReciveOrderList(reciveOrder);
        ExcelUtil<ReciveOrder> util = new ExcelUtil<ReciveOrder>(ReciveOrder.class);
        util.exportExcel(response, list, "财务收款单数据");
    }

    /**
     * 获取财务收款单详细信息
     */
    @RequiresPermissions("finance:reciveOrder:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        log.info("ReciveOrderController getInfo id:{}", id);
        return success(reciveOrderService.selectReciveOrderById(id));
    }
    /**
     * 获取今日销售收款单笔数及金额
     */
    @RequiresPermissions("finance:reciveOrder:query")
    @GetMapping("getSaleOrderReciveOrderCountAndMoney")
    public AjaxResult getSaleOrderReciveOrderCountAndMoney() {
        return success(reciveOrderService.getSaleOrderReciveCountAndMoney());
    }
    /**
     * 获取今日采购退货收款单笔数及金额
     */
    @RequiresPermissions("finance:reciveOrder:query")
    @GetMapping("getBuyBackReciveOrderCountAndMoney")
    public AjaxResult getBuyBackReciveOrderCountAndMoney() {
        return success(reciveOrderService.getBuyBackReciveOrderCountAndMoney());
    }
    /**
     * 获取收款单各阶段订单数
     */
    @RequiresPermissions("finance:reciveOrder:query")
    @GetMapping("getReceiveOrderThreeCountToIndex")
    public AjaxResult getReceiveOrderThreeCountToIndex() {
        return success(reciveOrderService.getReceiveOrderThreeCountToIndex());
    }
    /**
     * @return com.ruoyi.common.core.domain.R<com.ruoyi.system.api.domain.finance.ReciveOrder>
     * @description 根据财务收款单编号获取财务收款单线详情
     * @date 2024/11/11 下午4:56
     * @Param orderCode:
     **/
    @GetMapping("/get/byOrderCode/{orderCode}")
    public R<ReciveOrder> getReciveOrderByOrderCode(@PathVariable("orderCode") String orderCode) {
        log.info("ReciveOrderController getReciveOrderByOrderCode orderCode:{}", orderCode);
        return R.ok(reciveOrderService.selectReciveOrderByOrderCode(orderCode));
    }

    /**
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     * @description 审核财务收款单
     * @date 2024/11/11 下午5:02
     * @Param reciveOrder:
     **/
    @PostMapping("/audit/byOrderCode")
    public AjaxResult auditReciveOrderByOrderCode(@RequestBody ReciveOrder reciveOrder) {
        log.info("ReciveOrderController auditReciveOrderByOrderCode reciveOrder:{}", reciveOrder);
        AjaxResult ajaxResult = reciveOrderService.auditReciveOrderByOrderCode(reciveOrder);
        log.info("ReciveOrderController auditReciveOrderByOrderCode ajaxResult:{}", ajaxResult);
        return ajaxResult;
    }

    /**
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     * @description 根据收款单编号修改，sql语句修改已核销金额
     * @date 2024/11/19 上午9:26
     * @Param reciveOrder:
     **/
    @PostMapping("/updateReciveOrderByOrderCodeOfVerMoney")
    public AjaxResult updateReciveOrderByOrderCodeOfVerMoney(@RequestBody ReciveOrder reciveOrder) {
        log.info("ReciveOrderController updateReciveOrderByOrderCodeOfVerMoney reciveOrder:{}", reciveOrder);
        AjaxResult ajaxResult = reciveOrderService.updateReciveOrderByOrderCodeOfVerMoney(reciveOrder);
        log.info("ReciveOrderController updateReciveOrderByOrderCodeOfVerMoney ajaxResult:{}", ajaxResult);
        return ajaxResult;
    }

    /**
     * 新增财务收款单
     */
    @RequiresPermissions("finance:reciveOrder:add")
    @Log(title = "财务收款单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ReciveOrder reciveOrder) {
        log.info("ReciveOrderController add reciveOrder:{}", reciveOrder);
        AjaxResult ajaxResult = reciveOrderService.insertReciveOrder(reciveOrder);
        log.info("ReciveOrderController add ajaxResult:{}", ajaxResult);
        return ajaxResult;
    }

    /**
     * 修改财务收款单
     */
    @RequiresPermissions("finance:reciveOrder:edit")
    @Log(title = "财务收款单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ReciveOrder reciveOrder) {
        log.info("ReciveOrderController edit reciveOrder:{}", reciveOrder);
        AjaxResult ajaxResult = reciveOrderService.updateReciveOrder(reciveOrder);
        log.info("ReciveOrderController edit ajaxResult:{}", ajaxResult);
        return ajaxResult;
    }

    /**
     * 删除财务收款单
     */
    @RequiresPermissions("finance:reciveOrder:remove")
    @Log(title = "财务收款单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        log.info("ReciveOrderController remove ids:{}", Arrays.toString(ids));
        AjaxResult ajaxResult = reciveOrderService.deleteReciveOrderByIds(ids);
        log.info("ReciveOrderController remove ajaxResult:{}", ajaxResult);
        return ajaxResult;
    }

    /**
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     * @description 校验收款单编号
     * @date 2024/11/2 上午11:38
     * @Param orderCode:
     **/
//    @RequiresPermissions("finance:reciveOrder:check")
    @Log(title = "财务收款单", businessType = BusinessType.OTHER)
    @GetMapping("/checkOrderCode")
    public AjaxResult checkOrderCode(String orderCode) {
        log.info("ReciveOrderController checkOrderCode orderCode:{}", orderCode);
        AjaxResult ajaxResult = reciveOrderService.checkOrderCode(orderCode);
        log.info("ReciveOrderController checkOrderCode ajaxResult:{}", ajaxResult);
        return ajaxResult;
    }

    /**
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     * @description 获取所有支付方式列表
     * @date 2024/11/11 下午3:33
     **/
//    @RequiresPermissions("finance:payType:list")
    @GetMapping("/payType/listAll")
    public AjaxResult getPayTypeList() {
        log.info("ReciveOrderController getPayTypeList start...");
        AjaxResult ajaxResult = reciveOrderService.getPayTypeList();
        log.info("ReciveOrderController getPayTypeList ajaxResult:{}", ajaxResult);
        return ajaxResult;
    }


}
